package westh.ilib.struts.action.manage.answer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.util.YyAction;
import westh.util.YyDate;
import westh.util.YyID;

/**
 * @author zhangjian
 */
public class AnswerAddAction extends YyAction {
	//define commons log
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(AnswerAddAction.class);

	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// check log and authority,don`s delete	 
		westh.log.YyLog.addFootprint(request, "动作－添加答案");

		DynaActionForm f = (DynaActionForm) form;

		String questionId = f.getString("questionId");
		String answer = f.getString("answer");
		String answerer = f.getString("answerer");
		String answerTime = f.getString("answerTime");
		String answerRateAvg=f.getString("answerRateAvg");
		
	  //获得编号
	  String strId = YyID.getUUID32();
  	/*************************cdoi info start******************************/
	  String rId = "012f717f8dde4129a95837bf153de5a0"+strId;
	  //形成json进行封装 
	  JSONObject jsonCdoi = new JSONObject(); 
	  jsonCdoi.put("WESTDO_HANDLE",rId);                                    //唯一标识
	  jsonCdoi.put("CDOI_AGENCY_ID", "012f717f8dde4129a95837bf153de5a0");   //注册机构编码
	  jsonCdoi.put("DC_LANGUAGE", "c7d2ec860cee4b03ba845da0233ed977");      //语种标识符
	  jsonCdoi.put("CDOI_CUSTOMER_ID", "f41825a60cf44e83b83cbd9114220f29"); //用户标识符
	  jsonCdoi.put("DC_TYPE", "27278cfe6e1c429aa2197b3ff34a11ab");          //类型标识符
	  jsonCdoi.put("DC_TITLE", questionId);                                 //题名
	  jsonCdoi.put("DC_CREATOR", answerer);                                 //创建者
	  jsonCdoi.put("DC_SUBJECT", "");                                       //主题
	  jsonCdoi.put("DC_DESCRIPTION", answer);                               //描述
	  jsonCdoi.put("DC_PUBLISHER", "");                                     //出版者
	  jsonCdoi.put("DC_CONTRIBUTOR", "");                                   //其他责任者
	  jsonCdoi.put("DC_DATE", answerTime);                                  //日期
	  jsonCdoi.put("DC_FORMAT", "");                                        //格式
	  jsonCdoi.put("DC_IDENTIFIER", "");                                    //标识符
	  jsonCdoi.put("DC_SOURCE", "");                                        //来源
	  jsonCdoi.put("DC_RELATION", "");                                      //关联
	  jsonCdoi.put("DC_COVERAGE", "");                                      //覆盖范围                         
	  jsonCdoi.put("DC_RIGHTS", "");                                        //权限
	  jsonCdoi.put("CDOI_URL_SOURCE", "");                                  //资源URL
	  jsonCdoi.put("CDOI_STATUS", "1");                                     //资源注册状态，2审核通过
	  jsonCdoi.put("CDOI_NOTE", "");                                        //备注
	  
	  String cdoiJson = jsonCdoi.toString();
	  String cdoiId="";
	  try{
		  MuleClient clientW = new MuleClient();
//		  UMOMessage umo = clientW.send("axis:http://192.168.28.112:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
		  UMOMessage umo = clientW.send("axis:http://localhost:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
		  cdoiId = (String)umo.getPayload();
	  } catch (Exception e) {
		throw new RuntimeException(e);
	  }
		/** ****************************cdoi info end************************ */

		/** ****************************cdo info start************************ */
		JSONObject jsonObj = new JSONObject();

		jsonObj.put("answerId", cdoiId);                        
		jsonObj.put("questionId", questionId);                  
		jsonObj.put("answer", answer);              
		jsonObj.put("answerer", answerer);             
		jsonObj.put("answerTime", answerTime);               
		jsonObj.put("answerRateAvg", answerRateAvg);      
		String input = jsonObj.toString();

		String id = "";
		try {
			MuleClient clientD = new MuleClient();
			UMOMessage umo = clientD.send("axis:http://localhost:4381/LIBJson/iDlibJson?method=saveAnswer",input, null);
			id = (String) umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		/** ****************************cdo info end************************ */
		if (id.equals("1")) {
			String insertSubjectIndustry = "答案信息添加成功<br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../answerAddPre.do");
			return mapping.findForward("info");
		} else {
			String insertSubjectIndustry = "答案信息信息添加失败<br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../answerAddPre.do");
			return mapping.findForward("info");
		}
	}
}